package cpcn.dsp.demo.tx.liveness;

import com.alibaba.fastjson.JSONObject;
import cpcn.dsp.demo.consts.DSPConsts;
import cpcn.dsp.demo.util.HttpsUtil;
import cpcn.dsp.institution.api.security.EncryptAndDecrypt;
import cpcn.dsp.institution.api.system.DSPInstitutionEnvironment;
import cpcn.dsp.institution.api.tx.personalinfo.Tx2324Request;
import cpcn.dsp.institution.api.tx.personalinfo.Tx2324Response;
import cpcn.dsp.institution.api.util.Base64;
import cpcn.dsp.institution.api.util.GUIDGenerator;
import cpcn.dsp.institution.api.util.QRCodeUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: Test2324
 * @Description: 以人脸识别接口2324为例
 * @Date: 2020/7/2 0002 13:02
 * @Version: v1.0 文件初始创建
 */
public class Test2324 {


    public static void main(String[] args) throws Exception {

        //1.初始化
        DSPInstitutionEnvironment.initialize(DSPConsts.Keystore_test, DSPConsts.myKeystorePassword_test, DSPConsts.publicKey_test, DSPConsts.MSG_ENCRYPT_20);
        //1.1 encryptSN、signSN值不为空，说明初始化证书成功
        System.out.println(DSPInstitutionEnvironment.encryptSN);
        System.out.println(DSPInstitutionEnvironment.signSN);
        //2.生成请求明文
        JSONObject json = new JSONObject();
        Tx2324Request tx2324Request = new Tx2324Request();
        tx2324Request.setInstitutionID("100827");// 机构号由中金分配
        tx2324Request.setTxSN(GUIDGenerator.genGUID());
        tx2324Request.setName("张三");
        tx2324Request.setIdentificationNumber("110101198001010037");
        tx2324Request.setFrontUrl("http://localhost:8080/ReceiveNoticePage");//需要更换为外网可以访问的ip
        tx2324Request.setFrontMethod("0");
        tx2324Request.setReturnImage("1");
        tx2324Request.setBackUrl("http://localhost:8080/ReceiveNoticeBackground");//需要更换为外网可以访问的ip
        tx2324Request.setThreshold("");//默认为T5
        tx2324Request.setRemark("");//仅测试环境判断该字段1->不一致,2->未知，3->请求失败，其他->一致
        //报文处理
        tx2324Request.process();


        String plainText = tx2324Request.getRequestPlainText();
        System.out.println("请求明文：" + plainText);

        String reqMessage = tx2324Request.getRequestMessage();
        String reqSignature = tx2324Request.getRequestSignature();
        String requestDgtlEnvlp = tx2324Request.getRequestDgtlEnvlp();
        String requestSignSN = tx2324Request.getRequestSignSN();
        String requestEncryptSN = tx2324Request.getRequestEncryptSN();

        //生成参数
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("message", reqMessage);
        paramMap.put("signature", reqSignature);
        paramMap.put("dgtlenvlp", requestDgtlEnvlp);
        paramMap.put("signSN", requestSignSN);
        paramMap.put("encryptSN", requestEncryptSN);
        //生成请求参数
        String requestString = EncryptAndDecrypt.createLinkStringByGet(paramMap);
        System.out.println("请求密文：" + requestString);
        //请求接口
        String result = HttpsUtil.post(DSPConsts.TX_URL_TEST, requestString, null);
        System.out.println("响应密文：" + result);

        String responsePlainText = null;
        String[] respMsg = result.split(",");
        if (respMsg.length == 1 && -1 == respMsg[0].indexOf(',')) {
            responsePlainText = Base64.decode(respMsg[0], "UTF-8");
        } else if (respMsg.length == 5) {
            String responseMessage = respMsg[0];
            String responseSignature = respMsg[1];
            String responseDgtlEnvlp = respMsg[2];
            String respSignSN = respMsg[3];
            String respEncryptSN = respMsg[4];

            Tx2324Response tx2324Response = new Tx2324Response(responseMessage, responseSignature, responseDgtlEnvlp, respSignSN, respEncryptSN);
            responsePlainText = tx2324Response.getResponsePlainText();
            if ("2000".equals(tx2324Response.getCode())) {
                //响应中的URL是活体链接地址
                String url = tx2324Response.getUrl();
                //提供二维码生成方法。建议二维码大小为500，机构方可自己根据页面来调试生成的二维码大小
                String qrCode = QRCodeUtil.createQRCode(url, 500, 500);
                //此方法为生成携带logo的二维码链接，logo图片存放在SRC的同级目录下。
                String qrCodeWithLogo = QRCodeUtil.createQRCodeWithLogo(url, "cpcnLogo_02.jpg", 500, 500);
            }
        }
        System.out.println("响应明文：" + responsePlainText);
    }

}
